<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>首页</title>
		<link rel="stylesheet" type="text/css" href="css/iconfont.css" />
		<link rel="stylesheet" href="css/map.css">
		<link rel="stylesheet" href="css/mui.min.css">
	</head>

	<body>
		<div id="app">

		</div>

		<script src="js/mui.min.js"></script>
		<script src="js/util.js"></script>
		<script src="js/base.js"></script>
		<script type="text/javascript">
			mui.init();
			mui.plusReady(function() {
				
				//1.获取位置权限-如果没有获取就让他一直刷新页面从而获取
				plus.geolocation.getCurrentPosition(function(p) {
					// console.log('维度:' + p.coords.latitude + '\经度:' + p.coords.longitude + '\海拔:' + p.coords.altitude); 
					localStorage.setItem("get_loc", "1");
					var a = plus.webview.getWebviewById("html/home.html");
					mui.fire(a, 'home_reload', {})
				}, function(e) {
					mui.toast('请打开手机位置权限！否则会导致地图功能将无法使用');
					window.location.reload()
				}, {
					provider: 'baidu'
				});
				setTimeout(function() {
					// console.log(localStorage.getItem("get_loc"))
					if (!localStorage.getItem("get_loc")) {//如果他不存在让他刷新页面
						window.location.reload();
					}
					if (localStorage.getItem("get_loc") != 1) {//如果他不等于1让他刷新页面
						window.location.reload();
					}
				}, 1000)
				
				//2.查看是否打开GPS :安卓的手机
				function getGEO_status(){  
					var context = plus.android.importClass("android.content.Context");  
					var locationManager=plus.android.importClass("android.location.LocationManager");  
					var main=plus.android.runtimeMainActivity();  
					var mainSvr=main.getSystemService(context.LOCATION_SERVICE); 
					if(mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
						console.log("已经开启GPS");						
					}
					else{
						var r= confirm("请开启定位服务-5秒后程序关闭")
						if(r==true){
							setTimeout(function() {
								plus.runtime.quit();//关闭程序
								// plus.runtime.restart(); //重启程序
							}, 5000);
						}
						else{
							mui.toast("厕所查找功能将不能正常使用！");
						}
						
						
					}
				} 
				getGEO_status();



				// 判断当前网络状态
				get_connection();
				
				function get_connection() {
					var types = {};
					types[plus.networkinfo.CONNECTION_UNKNOW] = "Unknown connection";
					types[plus.networkinfo.CONNECTION_NONE] = "None connection";
					types[plus.networkinfo.CONNECTION_ETHERNET] = "Ethernet connection";
					types[plus.networkinfo.CONNECTION_WIFI] = "WiFi connection";
					types[plus.networkinfo.CONNECTION_CELL2G] = "Cellular 2G connection";
					types[plus.networkinfo.CONNECTION_CELL3G] = "Cellular 3G connection";
					types[plus.networkinfo.CONNECTION_CELL4G] = "Cellular 4G connection";
					if (types[plus.networkinfo.getCurrentType()] === "None connection" || types[plus.networkinfo.getCurrentType()] ===
						"Unknown connection") {
						plus.storage.setItem('network-status', 'false')
					} else {
						plus.storage.setItem('network-status', 'true')
					};
				};
				document.addEventListener("resume", function() {
					get_connection()
				}, false);
				document.addEventListener("netchange", function() {
					var nt = plus.networkinfo.getCurrentType();
					
					switch (nt) {
						case plus.networkinfo.CONNECTION_ETHERNET:
						case plus.networkinfo.CONNECTION_WIFI:
							plus.storage.setItem('network-status', 'true');
							mui.toast('您已切换至无线网络～');
							break;
						case plus.networkinfo.CONNECTION_CELL2G:
						case plus.networkinfo.CONNECTION_CELL3G:
						case plus.networkinfo.CONNECTION_CELL4G:
							plus.storage.setItem('network-status', 'true');
							mui.toast('您已切换至移动网络～');
							break;
						default:
							plus.storage.setItem('network-status', 'false');
							mui.toast('您已关闭网络～');
							break;
					};
				}, false);
				
				// 监听退出
				mui.back = function(){
				    var btn = ["确定","取消"];
					mui.confirm('确认退出程序？','无线经开',btn,function(e){
						if(e.index==0){
						
							function isExsitsAndroid(sSID) {
								console.log("查看以前是否也配置过这个网络" + sSID);
								//WifiConfiguration
								var MainActivity = plus.android.runtimeMainActivity();
								var Context = plus.android.importClass('android.content.Context');
								plus.android.importClass("android.net.wifi.WifiManager");
								plus.android.importClass("android.net.wifi.WifiInfo");
								var List = plus.android.importClass("java.util.List");
								var ArrayList = plus.android.importClass("java.util.ArrayList");
								var wifiManager = MainActivity.getSystemService(Context.WIFI_SERVICE);
								
								var existingConfigs = new ArrayList();
								existingConfigs = wifiManager.getConfiguredNetworks();
								if (existingConfigs.size() != 0) {
									for (var i = 0; i < existingConfigs.size(); i++) {
										if (existingConfigs.get(i).plusGetAttribute('SSID') == ("\"" + sSID + "\"")) {
											console.log("该制定的ssid存在于配置中:" + sSID);
											return existingConfigs.get(i);
										}
									}
								}
								console.log("该ssid没有配置过")
								return null;
							}
						
							//下面为关闭程序将清除保存的wifi
							var MainActivity = plus.android.runtimeMainActivity();
							var Context = plus.android.importClass('android.content.Context');
							plus.android.importClass("android.net.wifi.WifiManager");
							plus.android.importClass("android.net.wifi.WifiInfo");
							plus.android.importClass("android.net.wifi.WifiConfiguration");
							var wifiManager = MainActivity.getSystemService(Context.WIFI_SERVICE);
							
							var WifiConfiguration = tempConfig = isExsitsAndroid('testwifi');
							
							if (tempConfig != null) {
								console.log("删除原来连接的wifi" + tempConfig);
								wifiManager.removeNetwork(tempConfig.plusGetAttribute('networkId'));
								//关闭程序
								plus.runtime.quit();
							}
						
							
						}
					});
				}
				
				
			})
		</script>
	</body>

</html>
